package com.zt.dev.accessProduct.controller;


import com.zt.dev.accessProduct.pojo.AccessProductEnum;
import com.zt.dev.accessProduct.pojo.AccessProductReport;
import com.zt.dev.accessProduct.pojo.AccessProductTemplate;
import com.zt.dev.accessProduct.service.AccessProductService;
import com.zt.dev.accessProduct.util.ExcelUtil;
import com.zt.dev.common.ServerResponse;
import com.zt.dev.siteInfoRegister.pojo.Site;
import com.zt.dev.siteInfoRegister.pojo.SiteInfoEnum;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.List;

@RestController
@RequestMapping("/accessProduct")
public class AccessProductController {
    @Autowired
    private AccessProductService accessProductService;

    private final static String title = "评估产品";

    /*
    * 评估产品生成与管理工具支持基于评估模型、评估计算结果、
    * 评估分析结果等要素自动生成SY评估产品，具有评估产品生成、编辑、导入、导出等功能。
    */

    /**
     * 查看所有模板
     * @return
     */
    @RequestMapping(value = "/getAllTemplate")
    @ResponseBody
    public ServerResponse getAllTemplate(@RequestParam(value = "currentPage")Integer currentPage,
                                         @RequestParam(value = "pageSize")Integer pageSize){
        List<AccessProductTemplate> accessProductTemplates = accessProductService.getAllTemplatePage(currentPage,pageSize);
        return ServerResponse.createBySuccess(accessProductTemplates);
    }


    /**
     * 根据id查找模板
     * @param id
     * @return
     */
    @RequestMapping(value = "/getTemplateById")
    @ResponseBody
    public ServerResponse getTemplateById(@RequestParam(value = "id") Integer id){
        if(!StringUtils.isEmpty(id)){
            AccessProductTemplate accessProductTemplate = accessProductService.getTemplateById(id);
            return ServerResponse.createBySuccess(accessProductTemplate);
        }
        return ServerResponse.createByErrorMessage("ID为空，请重试");
    }


    /**
     * 添加模板
     * @param accessProductTemplate
     * @return
     */
    @RequestMapping(value = "/addTemplate")
    @ResponseBody
    public ServerResponse addTemplate(AccessProductTemplate accessProductTemplate){
        if(!StringUtils.isEmpty(accessProductTemplate.getName())){
            int index = accessProductService.addTemplate(accessProductTemplate);
            if (index!=1){
                return ServerResponse.createByErrorMessage("添加失败，请重试！");
            }
            return ServerResponse.createBySuccessMessage("添加成功");
        }
        return ServerResponse.createByErrorMessage("请检查模板参数");
    }


    /**
     * 删除模板
     * @param id
     * @return
     */
    @RequestMapping(value = "/removeTemplate")
    @ResponseBody
    public ServerResponse removeTemplate(@RequestParam(value = "id") Integer id){
        if(!StringUtils.isEmpty(id)){
            int index = accessProductService.removeTemplate(id);
            if (index!=1){
                return ServerResponse.createByErrorMessage("删除失败");
            }
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("请勾选删除的模板id");
    }


    @RequestMapping(value = "/modifyTemplate")
    @ResponseBody
    public ServerResponse modifyTemplate(AccessProductTemplate accessProductTemplate){
        if(!StringUtils.isEmpty(accessProductTemplate)){
            int index = accessProductService.modifyTemplate(accessProductTemplate);
            if (index!=1){
                return ServerResponse.createByErrorMessage("修改失败，请重试！");
            }
            return ServerResponse.createBySuccessMessage("修改成功");
        }
        return ServerResponse.createByErrorMessage("请检查模板参数");
    }

    @RequestMapping(value = "/getAllReport")
    @ResponseBody
    public ServerResponse getAllReport(@RequestParam(value = "currentPage")Integer currentPage,
                                       @RequestParam(value = "pageSize")Integer pageSize){
        List<AccessProductReport> accessProductReports = accessProductService.getAllReportPage(currentPage,pageSize);
        return ServerResponse.createBySuccess(accessProductReports);
    }

    @RequestMapping(value = "/getReportById")
    @ResponseBody
    public ServerResponse getReportById(@RequestParam(value = "id") Integer id){
        if(!StringUtils.isEmpty(id)){
            AccessProductReport accessProductReport = accessProductService.getReportById(id);
            return ServerResponse.createBySuccess(accessProductReport);
        }
        return ServerResponse.createByErrorMessage("ID为空，请重试");
    }

    @RequestMapping(value = "/addReport")
    @ResponseBody
    public ServerResponse addReport(AccessProductReport accessProductReport){
        if(!StringUtils.isEmpty(accessProductReport.getName())){
            int index = accessProductService.addReport(accessProductReport);
            if (index!=1){
                return ServerResponse.createByErrorMessage("添加失败，请重试！");
            }
            return ServerResponse.createBySuccessMessage("添加成功");
        }
        return ServerResponse.createByErrorMessage("请检查报告内容");
    }

    @RequestMapping(value = "/removeReport")
    @ResponseBody
    public ServerResponse removeReport(@RequestParam(value = "id") Integer id){
        if(!StringUtils.isEmpty(id)){
            int index = accessProductService.removeReport(id);
            if (index!=1){
                return ServerResponse.createByErrorMessage("删除失败");
            }
            return ServerResponse.createBySuccessMessage("删除成功");
        }
        return ServerResponse.createByErrorMessage("请勾选删除的报告id");
    }

    @RequestMapping(value = "/modifyReport")
    @ResponseBody
    public ServerResponse modifyReport(AccessProductReport accessProductReport){
        if(!StringUtils.isEmpty(accessProductReport)){
            int index = accessProductService.modifyReport(accessProductReport);
            if (index!=1){
                return ServerResponse.createByErrorMessage("修改失败，请重试！");
            }
            return ServerResponse.createBySuccessMessage("修改成功");
        }
        return ServerResponse.createByErrorMessage("请检查报告参数");
    }

    /**
     * 导出
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/exportAP")
    public void exportAP(HttpServletResponse response)throws Exception{
        String fileName = title + ".xls";
        List<AccessProductTemplate> accessProductTemplates = accessProductService.getAllTemplate();

        String[] title1 = new String[]{"序号,1,2","场地信息,2,1","使用部队,1,1","使用具体信息,3,1","用户信息,6,1","任务信息,3,1"};
        String[] title2 = new String[]{"场地名称", "场地地址", "使用部队", "使用状态", "开始时间", "结束时间", "用户名", "性别", "职位", "单位", "专业", "联系方式", "任务信息", "任务分配情况", "研发单位"};
        String[] column = new String[]{"name", "address", "troops", "state", "startTime","finishTime","userName","sex","duty","unit","major","tel","taskMsg","taskAllot","developUnit"};
        HSSFWorkbook wb = new ExcelUtil().getHSSFWorkbook(title, title1, title2, accessProductTemplates, column);
        fileName = new String(fileName.getBytes(),"ISO8859-1");
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        response.addHeader("Pargam","no-cache");
        response.addHeader("Cache-Control","no-cache");
        OutputStream os = response.getOutputStream();
        wb.write(os);
        os.flush();
        os.close();
    }


    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/importAP")
    public ServerResponse importAP(MultipartFile file)throws Exception{
        ExcelUtil excelUtil = new ExcelUtil(AccessProductEnum.objMap);
        try {
            saveData(excelUtil,file);

            if(excelUtil.getRowNum() != excelUtil.getTotalRowNum()){
                saveData(excelUtil,file);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMessage("导入数据时出现错误！");
        }
        return ServerResponse.createBySuccess("导入成功");
    }


    private void saveData(ExcelUtil excelUtil, MultipartFile file) throws Exception {
        List<Object> list = excelUtil.getDataFromExcel(file, AccessProductTemplate.class);
        for(int i =0;i<list.size();i++){
            AccessProductTemplate accessProductTemplate = (AccessProductTemplate) list.get(i);

            accessProductService.addTemplate(accessProductTemplate);
        }
        System.gc();
    }



}
